package cetcbigdata.da.service.beijing.administrative_power_city_region;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YuanGongPing
 * @version 0.1
 * @description SpiderBeiJing
 * @since 2022/9/14 15:58
 */


public class SpiderBeiJing extends Base {
    private static String region = "北京市";

    private static String module = "服务事项-行政权力清单-市区共有权力清单";

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {
        this.parseListPage(recordSender, fieldsList);
    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return processResult("xxx", "xxx", "xxx", "xxx", new JSONObject(), "xxx", "xxx");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private void parseListPage(RecordSender recordSender, List<Object> fieldsList){
        // 初始化总页码数
        int pages = 0;
        // 当前页
        int currentPage = 1;
        // 如果当前也不等于总页码数，则一直循环 ，实现翻页
        while (currentPage != pages){
            try {
                String listUrl = "https://banshi.beijing.gov.cn/guideservice/powerList/getList?useLevel=2&taskName=&deptCode=&taskType=&pageNum=" + currentPage;;
                JSONObject headers = new JSONObject();
                headers.put("Referer", "http://banshi.beijing.gov.cn/pubtask/frfw.html?locationCode=110000000000");
                headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36");
                JSONObject parmas = new JSONObject();
                // 请求列表页
                String listResponse =  getReq(listUrl, headers, parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);

                if(responseObj.getString("code").equals("200")){
                    JSONArray list = responseObj.getJSONObject("data").getJSONArray("list");
                    for(Object o : list){
                        JSONObject item = (JSONObject) o;
                        String title = item.getString("taskName");
                        String detailUrl = String.format("https://banshi.beijing.gov.cn/guideservice/powerList/getInfo?id=%s",
                                item.getString("id")
                        );
                        String detailResponse =  getReq(detailUrl, headers, parmas);
                        JSONObject content = UtilBeiJing.getInfo(detailResponse);
                        processItem(
                                processResult(listUrl, listResponse, detailUrl, detailResponse, content, region, module),
                                recordSender,
                                fieldsList
                        );
                    }
                }
                // 更新总页数
                pages = responseObj.getJSONObject("data").getInteger("pages");
                if(currentPage >= pages){
                    return;
                }
                // 更新当前页
                currentPage = responseObj.getJSONObject("data").getInteger("pageNum") + 1;

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        SpiderBeiJing spider = new SpiderBeiJing();
        spider.getData(null, new ArrayList<>());
    }
}
